package com.digua.tank;

import javax.swing.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author wangdigua
 * @title 声音辅助类
 * @date 2020/11/7 9:15 下午
 * @since 1.0.0
 */
public class Audio extends JFrame {

    private String file ;

    public Audio(String file) throws HeadlessException {
        this.file = file;
    }

    public static void main(String[] args) {
//        Audio a = new Audio("audio/war1.wav");
        Audio a = new Audio("audio/explode.wav");
        a.player();
    }

    public void player(){
        try {
            URL cb;
            String path = Thread.currentThread().getContextClassLoader().getResource(file).getPath();
            File f = new File(path); //引号里面的是音乐文件所在的绝对路径
            cb = f.toURL();
            AudioClip aau;
            aau = Applet.newAudioClip(cb);//加载音频
            aau.play(); //播放音频
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}
